<!DOCTYPE html>
<html lang="Chinese, English">
<head>

    <!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="Green" />



<meta name="description" content="D3.js是一个通过数据来操作文档JavaScript库。结合SVG，D3特别适合用于图表的绘制，但D3本身并不是一个图表库，它把数据绑定到DOM中，利用SVG输出图表。本文所讲内容适用于v4.0+版本
从v4.0开始，D3把各个模块分离开来，各个模块相互独立，也就是说如果你只需要用到D3强大的数组操作部分，只需引用d3-array部分(例如npm install d3-array)，不需要引用所">
<meta property="og:type" content="article">
<meta property="og:title" content="D3.js入门">
<meta property="og:url" content="https://greenmzc.github.io/2016/10/11/D3-js入门/index.html">
<meta property="og:site_name" content="Green的博客 | Green's Blog">
<meta property="og:description" content="D3.js是一个通过数据来操作文档JavaScript库。结合SVG，D3特别适合用于图表的绘制，但D3本身并不是一个图表库，它把数据绑定到DOM中，利用SVG输出图表。本文所讲内容适用于v4.0+版本
从v4.0开始，D3把各个模块分离开来，各个模块相互独立，也就是说如果你只需要用到D3强大的数组操作部分，只需引用d3-array部分(例如npm install d3-array)，不需要引用所">
<meta property="og:image" content="https://greenmzc.github.io/img/2016101301.png">
<meta property="og:image" content="https://greenmzc.github.io/img/2016101302.png">
<meta property="og:image" content="https://greenmzc.github.io/img/2016101303.png">
<meta property="og:image" content="https://greenmzc.github.io/img/2016101304.png">
<meta property="og:image" content="https://greenmzc.github.io/img/2016101401.png">
<meta property="og:image" content="https://greenmzc.github.io/img/2016101402.png">
<meta property="og:image" content="https://greenmzc.github.io/img/2016101403.png">
<meta property="og:updated_time" content="2016-12-27T03:05:04.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="D3.js入门">
<meta name="twitter:description" content="D3.js是一个通过数据来操作文档JavaScript库。结合SVG，D3特别适合用于图表的绘制，但D3本身并不是一个图表库，它把数据绑定到DOM中，利用SVG输出图表。本文所讲内容适用于v4.0+版本
从v4.0开始，D3把各个模块分离开来，各个模块相互独立，也就是说如果你只需要用到D3强大的数组操作部分，只需引用d3-array部分(例如npm install d3-array)，不需要引用所">
<meta name="twitter:image" content="https://greenmzc.github.io/img/2016101301.png">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">


    <link rel="alternate" href="/atom.xml" title="Green的博客 | Green&#39;s Blog" type="application/atom+xml">



    <link rel="shortcut icon" href="/favicon.png">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">



    <link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">



    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/green/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="/css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>D3.js入门 | Green的博客 | Green&#39;s Blog</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: true,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: undefined
    }
</script>


    <script> yiliaConfig.jquery_ui = [false]; </script>



    <script> yiliaConfig.rootUrl = "\/";</script>



    <script src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-56ff76c972fc0cf9"></script>



    <script>
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "//hm.baidu.com/hm.js?74f5c9dc68aa8b1babf7bd3c11285f9b";
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(hm, s);
        })();
    </script>


</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            <img src="/img/avatar.png" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/">Green</a></h1>
        </hgroup>

        

        


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/">主页</a></li>
                        
                            <li><a href="/archives/">所有文章</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" href="mailto:15869186780@163.com" title="Email"></a>
                            
                                <a class="fa GitHub" href="https://github.com/Greenmzc" title="GitHub"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/D3/">D3</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JavaScript/">JavaScript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/blog/">blog</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/css/">css</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/shell/">shell</a></li></ul>
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a class="main-nav-link switch-friends-link" href="http://fangyi.me">Phieo</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://excaliburhan.com">虚夜宫</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://www.ggfly.me">郁川溪畔</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://nickliuyong.github.io/blogs/">NickLiu</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://blog.icdoit.com">没事溜达</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://www.lixuejiang.me">风君子</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">专注于前端</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">Green</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                <img src="/img/avatar.png" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="回到主页">Green</a></h1>
            </hgroup>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/">主页</a></li>
                
                    <li><a href="/archives/">所有文章</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" target="_blank" href="mailto:15869186780@163.com" title="Email"></a>
                            
                                <a class="fa GitHub" target="_blank" href="https://github.com/Greenmzc" title="GitHub"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="标签" friends="友情链接" about="关于我"/>
</nav>
      <div class="body-wrap"><article id="post-D3-js入门" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/2016/10/11/D3-js入门/" class="article-date">
      <time datetime="2016-10-11T09:34:15.000Z" itemprop="datePublished">2016-10-11</time>
</a>


    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      D3.js入门
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        

        
    <div class="article-tag tagcloud">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/D3/">D3</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JavaScript/">JavaScript</a></li></ul>
    </div>

        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <p>D3.js是一个通过数据来操作文档JavaScript库。结合SVG，D3特别适合用于图表的绘制，但D3本身并不是一个图表库，它把数据绑定到DOM中，利用SVG输出图表。本文所讲内容适用于v4.0+版本</p>
<p>从v4.0开始，D3把各个模块分离开来，各个模块相互独立，也就是说如果你只需要用到D3强大的数组操作部分，只需引用<code>d3-array</code>部分(例如npm install d3-array)，不需要引用所有的D3库。这样做能够使前端模块化开发更加灵活，减小引用资源体积，节省性能</p>
<a id="more"></a>
<h3 id="用D3操作DOM元素"><a href="#用D3操作DOM元素" class="headerlink" title="用D3操作DOM元素"></a>用D3操作DOM元素</h3><ul>
<li><code>d3.select(selector)</code> 返回匹配选择器的第一个元素</li>
<li><code>d3.selectAll(selector)</code> 返回匹配选择器所有元素</li>
</ul>
<p>比如d3.selectAll(‘p’)可以选择所有的p元素，d3.select(‘p’)只选择第一个p元素</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">d3.select(<span class="string">'p'</span>)</div><div class="line">d3.selectAll(<span class="string">'p'</span>)</div></pre></td></tr></table></figure>
<p>还可以选择其他常用的选择器，包括派生选择器，与document.querySelector和document.querySelectorAll方法十分类似</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">d3.select(<span class="string">'.chart'</span>)</div><div class="line">d3.selectAll(<span class="string">'.chart'</span>)</div><div class="line">d3.select(<span class="string">'#chart'</span>)</div><div class="line">d3.select(<span class="string">'.chart rect'</span>)</div></pre></td></tr></table></figure>
<ul>
<li><code>selection.append(type)</code> 创建新元素并返回该元素</li>
<li><code>selection.remove()</code> 从DOM中移除当前元素</li>
<li><code>selection.attr(name[,value])</code> 取得或设置属性的值</li>
<li><code>selection.style(name[,value[,priority]])</code> 取得或设置元素的样式</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">d3.select(<span class="string">'body'</span>)</div><div class="line">  .append(<span class="string">'svg'</span>)</div><div class="line">  .attr(<span class="string">'width'</span>, <span class="number">500</span>)</div><div class="line">  .attr(<span class="string">'height'</span>, <span class="number">500</span>)</div><div class="line">  .style(<span class="string">'background'</span>, <span class="string">'red'</span>)</div><div class="line">  .remove()</div></pre></td></tr></table></figure>
<p>上面的代码首先在body中创建了一个svg标签，然后给它设置高度和宽度，设置背景色，最后被删除，D3的代码风格基本采用链式写法，因为大部分D3的方法都返回D3对象的实例，这和jQuery非常相似</p>
<h3 id="在DOM元素中绑定数据"><a href="#在DOM元素中绑定数据" class="headerlink" title="在DOM元素中绑定数据"></a>在DOM元素中绑定数据</h3><p>数据绑定是D3的最大特色，也是最让入门者困惑的地方，D3中文全称“数据驱动文档”，主要的方法有两个</p>
<ul>
<li><code>selection.datum([value])</code> 为每个元素绑定值value</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>Dog<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>Cat<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>Monkey<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="javascript"></span></div><div class="line">    <span class="keyword">var</span> p = d3.select(<span class="string">'body'</span>).selectAll(<span class="string">'p'</span>);</div><div class="line">    <span class="comment">// 为p元素集绑定数值1</span></div><div class="line">    p.datum(<span class="number">1</span>);</div><div class="line">    <span class="built_in">console</span>.log(p);</div><div class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div></pre></td></tr></table></figure>
<p>运行代码查看控制台，数值1已经被赋予了p标签的<code>__data__</code>属性了<br><img src="/img/2016101301.png" width="300" height="150" title="2016101301"></p>
<ul>
<li><code>selection.data([data[,key]])</code> 为每个元素分别绑定对应data中数据值</li>
</ul>
<p>data方法与datum方法的区别在于，data的参数是数组，分别分配给对应的DOM元素，每个DOM元素得到一个value值，而datum把相同参数均赋值给DOM元素，每个元素得到的数据值相同，data方法比较适用于日常数据需求，使用更多</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>Dog<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>Cat<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>Monkey<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="javascript"></span></div><div class="line">    <span class="keyword">var</span> dataset = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</div><div class="line">    <span class="keyword">var</span> p = d3.select(<span class="string">'body'</span>).selectAll(<span class="string">'p'</span>);</div><div class="line">    <span class="comment">// 为p元素集绑定数组dataset</span></div><div class="line">    p.data(dataset);</div><div class="line">    <span class="built_in">console</span>.log(p);</div><div class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div></pre></td></tr></table></figure>
<p>运行代码查看控制台，数值1、2、3分别绑定到了第1、2、3个p标签<br><img src="/img/2016101302.png" width="200" height="40" title="2016101302"><br><img src="/img/2016101303.png" width="200" height="40" title="2016101303"><br><img src="/img/2016101304.png" width="200" height="40" title="2016101304"></p>
<p>绑定数据后，就可以利用绑定的数据做一些事情了，D3为大部分操作DOM的方法添加了函数参数，该函数有两个参数，一个代表绑定的数据，通常为<code>d</code>，另一个为序数值，通常为<code>i</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>Dog<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>Cat<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>Monkey<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="javascript"></span></div><div class="line">    <span class="keyword">var</span> dataset = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</div><div class="line">    <span class="keyword">var</span> p = d3.select(<span class="string">'body'</span>).selectAll(<span class="string">'p'</span>);</div><div class="line">    <span class="comment">// 更改p元素内的内容,d是数据值，i是序数值</span></div><div class="line">    p.data(dataset)</div><div class="line">      .text(<span class="function"><span class="keyword">function</span>(<span class="params">d, i</span>) </span>&#123;</div><div class="line">        <span class="keyword">return</span> d + <span class="string">' '</span> + i</div><div class="line">      &#125;);</div><div class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div></pre></td></tr></table></figure>
<p>运行结果如下图：<br><img src="/img/2016101401.png" width="40" height="100" title="2016101401"></p>
<h3 id="enter-与exit-的用法"><a href="#enter-与exit-的用法" class="headerlink" title="enter()与exit()的用法"></a>enter()与exit()的用法</h3><p>在前面讲数据绑定中，数组的长度与标签数量是相等的，如果数据的长度不可控，会导致有些标签没有数据，有些数据没有标签匹配，这个时候就要用到enter与exit方法了</p>
<ul>
<li><code>selection.enter()</code> 为缺失的元素返回占位符</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>Dog<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="javascript"></span></div><div class="line">    <span class="keyword">var</span> dataset = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</div><div class="line">    <span class="keyword">var</span> p = d3.select(<span class="string">'body'</span>).selectAll(<span class="string">'p'</span>);</div><div class="line">    <span class="comment">// 目前只有1个元素，3个数据值</span></div><div class="line">    enter = p.data(dataset).enter();</div><div class="line">    <span class="built_in">console</span>.log(enter);</div><div class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div></pre></td></tr></table></figure>
<p>查看控制台，发现出现了2个并不真实存在的EnterNode，绑定了剩余的数据，这就是enter部分，如果后面继续添加新的标签，标签会被自动绑定剩余数据<br><img src="/img/2016101402.png" width="400" height="350" title="2016101402"></p>
<ul>
<li><code>selection.exit()</code> 返回没有被绑定数据的元素</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>Dog<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>Cat<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>Monkey<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="javascript"></span></div><div class="line">    <span class="keyword">var</span> dataset = [<span class="number">1</span>];</div><div class="line">    <span class="keyword">var</span> p = d3.select(<span class="string">'body'</span>).selectAll(<span class="string">'p'</span>);</div><div class="line">    <span class="comment">// 目前有3个元素，只有1个数据值</span></div><div class="line">    exit = p.data(dataset).exit();</div><div class="line">    <span class="built_in">console</span>.log(exit);</div><div class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div></pre></td></tr></table></figure>
<p>查看控制台，有2个标签并没有绑定数据，这就是exit部分，可以把多余的exit部分移除掉<br><img src="/img/2016101403.png" width="180" height="165" title="2016101403"></p>
<h3 id="交互与动画"><a href="#交互与动画" class="headerlink" title="交互与动画"></a>交互与动画</h3><ul>
<li><code>selection.on(typenames[,listener[,capture]])</code></li>
</ul>
<p>D3的事件绑定非常简单，与jQuery的on方法一样，指定事件类型后，调用回调函数</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>Dog<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="javascript"></span></div><div class="line">    <span class="keyword">var</span> p = d3.select(<span class="string">'body'</span>).select(<span class="string">'p'</span>);</div><div class="line">    p.on(<span class="string">'click'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">      d3.select(<span class="keyword">this</span>).text(<span class="string">'Cat'</span>);</div><div class="line">    &#125;)</div><div class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div></pre></td></tr></table></figure>
<ul>
<li><code>selection.transition([name])</code></li>
</ul>
<p>在元素之间加入transition方法，就能形成炫酷的过渡动画</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="javascript"></span></div><div class="line">    <span class="keyword">var</span> div = d3.select(<span class="string">'body'</span>).select(<span class="string">'div'</span>);</div><div class="line">    div.style(<span class="string">'width'</span>, <span class="string">'200px'</span>)</div><div class="line">      .style(<span class="string">'height'</span>, <span class="string">'200px'</span>)</div><div class="line">      .style(<span class="string">'background'</span>, <span class="string">'red'</span>)</div><div class="line">      .transition()</div><div class="line">      .style(<span class="string">'width'</span>, <span class="string">'400px'</span>);</div><div class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div></pre></td></tr></table></figure>

      
    </div>
    
  </div>
  
    


    <nav id="article-nav">
        
            <div id="article-nav-newer" class="article-nav-title">
                <a href="/2016/10/18/D3-js上手——折线图/">
                    D3.js上手——折线图
                </a>
            </div>
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="/2016/10/06/开始使用hexo搭建博客/">
                    开始使用hexo搭建博客
                </a>
            </div>
        
    </nav>

  
</article>

    <div id="toc" class="toc-article">
        <strong class="toc-title">文章目录</strong>
        
            <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#用D3操作DOM元素"><span class="toc-number">1.</span> <span class="toc-text">用D3操作DOM元素</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#在DOM元素中绑定数据"><span class="toc-number">2.</span> <span class="toc-text">在DOM元素中绑定数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#enter-与exit-的用法"><span class="toc-number">3.</span> <span class="toc-text">enter()与exit()的用法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#交互与动画"><span class="toc-number">4.</span> <span class="toc-text">交互与动画</span></a></li></ol>
        
    </div>
    <style>
        .left-col .switch-btn,
        .left-col .switch-area {
            display: none;
        }
        .toc-level-3 i,
        .toc-level-3 ol {
            display: none !important;
        }
    </style>

    <input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">

    <script>
        yiliaConfig.toc = ["隐藏目录", "显示目录", !!"false"];
    </script>



    
<div class="share">
    

    
        <div class="addthis_sharing_toolbox"></div>
    
</div>




    
      <div class="duoshuo" id="comments">
    <div id="comment-box" ></div>
    <div class="ds-thread" id="ds-thread" data-thread-key="2016/10/11/D3-js入门/" data-title="D3.js入门" data-url="https://greenmzc.github.io/2016/10/11/D3-js入门/"></div>
    <script>
        var duoshuoQuery = {short_name:"greenmzc"};
        var loadComment = function(){
            var d = document, s = d.createElement('script');
            s.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
            s.async = true; s.charset = 'UTF-8';
            (d.head || d.body).appendChild(s);
        }

        
    </script>
    
    <script> loadComment(); </script>

</div>
    




    <div class="scroll" id="post-nav-button">
        
            <a href="/2016/10/18/D3-js上手——折线图/" title="上一篇: D3.js上手——折线图">
                <i class="fa fa-angle-left"></i>
            </a>
        

        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="/2016/10/06/开始使用hexo搭建博客/" title="下一篇: 开始使用hexo搭建博客">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/2016/12/14/CSS背景渐变/">CSS背景渐变</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/11/07/D3-js上手——饼状图/">D3.js上手——饼状图</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/10/18/D3-js上手——折线图/">D3.js上手——折线图</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/10/11/D3-js入门/">D3.js入门</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/10/06/开始使用hexo搭建博客/">开始使用hexo搭建博客</a></li></ul>




    <script>
        
    </script>

</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i>
                2016-2017 Green
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="快速、简洁且高效的博客框架">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="简而不减 Hexo 双栏博客主题  v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" title="本站到访数"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>| </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit"  title="本页阅读量"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>

    </div>
    
<script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>





<div class="scroll" id="scroll">
    <a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="查看评论"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    // Open in New Window
    
        var oOpenInNew = {
            
            
            
            
            
            
             archives: ".archive-article-title", 
             miniArchives: "a.post-list-link", 
            
             friends: "#js-friends a", 
             socail: ".social a" 
        }
        for (var x in oOpenInNew) {
            $(oOpenInNew[x]).attr("target", "_blank");
        }
    
</script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>